<template>
  <div class="person">
    <h1>情况三：监视【reactive】定义的【对象类型】数据</h1>
    <h2>姓名：{{ person.name }}</h2>
    <h2>年龄：{{ person.age }}</h2>
    <button @click="changeName">修改姓名</button>
    <button @click="changeAge">修改年龄</button>
    <button @click="changePerson">修改整个人</button>
    <hr />
    <h2>测试：{{ obj.a.b.c }}</h2>
    <button @click="test">点我修改obj.a.b.c</button>
  </div>
</template>

<script setup name="Person">
import { reactive, watch } from "vue";

let person = reactive({
  name: "张三",
  age: 19,
});

let obj = reactive({
  a: {
    b: {
      c: 666,
    },
  },
});

function changeName() {
  person.name += "~";
}

function changeAge() {
  person.age += 1;
}

function changePerson() {
  //person = { name: "李四", age: 25 };
  Object.assign(person, { name: "李四", age: 20 });
}

function test() {
  obj.a.b.c = 888;
}
//情况三，监视【reactive】定义的【对象类型】数据，且默认开启深度监视，这种深度监视是不能关闭的
watch(person, (newValue, oldValue) => {
  console.log("person 变化了", newValue, oldValue);
});

watch(obj, (newValue, oldValue) => {
  console.log("obj 变化了", newValue, oldValue);
});
</script>

<style scoped>
.person {
  background-color: #c2e0f6;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  padding: 20px;
}
button {
  margin: 0 5px;
}
</style>